<template>
  <div class="container">
    <el-row>
      <el-form ref="searchform" :model="form" size="small">
        <el-col :span="3">
          <el-form-item prop="keyWord" >
            <el-input placeholder="姓名/电话" clearable v-model="form.keyWord"  style="width: 95%">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item  prop="department_id">
            <el-select
              v-model="form.department_id"
              placeholder="请选择上级部门"
              style="width: 95%"
            >
              <el-option
                v-for="item in prepareLineOption"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item  prop="status">
            <el-select
              v-model="form.status"
              placeholder="请选择状态"
              style="width: 95%"
            >
              <el-option
                v-for="item in statusOption"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <div class="rightbtn">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="searchlist"
            class="marginL"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh-left" size="small" @click="resetform"
            >重置</el-button
          >
        </div>
      </el-form>
    </el-row>
    <el-row>
      <el-button type="primary" size="small" @click="addItem">添加</el-button>
    </el-row>
    <el-row>
      <el-table
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px"
        size="small"
      >
        <el-table-column
          prop="nickname"
          label="用户名"
          show-overflow-tooltip
        ></el-table-column>

        <el-table-column prop="department_id" label="部门" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="userRole" label="职务" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="state" label="在线状态" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="status" label="状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-switch
              class="switch"
              v-model="scope.row.status"
              @change="changestatus(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-text="启用"
              inactive-text="禁用"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="update_time"
          label="更新时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="last_time"
          label="最近登录时间"
          show-overflow-tooltip
        ></el-table-column>

        <el-table-column prop="date" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="editItem(scope.row)">编辑</el-button>
      
            <el-button type="text" @click="deleteTableItem(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row>
        <el-col :span="24" class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 20]"
            :page-size="10"
            background
            :current-page.sync="page"
            layout="total, sizes, prev, pager, next"
            :total="pagination.total"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-row>
    <!-- 弹框 -->
    <subdialog
      :isshowDialog="isshowDialog"
      :dialogTitle="dialogTitle"
      :formType="formtype"
      :prepareLineOption="prepareLineOption"
      :userRoleOption="userRoleOption"
      @confirm="confirm"
      @closedialog="closedialog"
      :dialogdata="dialogdata"
    ></subdialog>

  </div>
</template>

<script type="text/javascript">
import {
  userList,
  userRoleOption,
 userUpdatestatus,
 userDelete,
 userRead,
 departmentOption
} from "@/api/userManagement";
import subdialog from "./components/dialog";
  //引入

export default {
  data() {
    return {
      isshowDialog: false,
      dialogTitle: "新增",
      formtype: 0,
      page: 1,
      rows: 10,
      form: {
        state: null,
      },
      tableData: [],
      category: [],
      prepareLineOption: [],
      userRoleOption: [],
      statusOption:[
        {title:'正常',id:1},
        {title:'禁止',id:0}
      ],
      pagination: {},
      dialogdata: null,
    };
  },
  components: {
    subdialog,

  },
  created() {
    this.getData();
    this.getprepareLineOption();
    this.getuserRoleOption();
  },
  methods: {
    async deleteTableItem(val) {
      let data = await userDelete({ id: val.id });
      if (data.code == "0000") {
        this.$message({
          type: "success",
          message: data.msg,
        });
        this.getData();
      }
    },
    //切换类型
    async changestatus(val) {
      let status = val.status == 0 ? 1 : 0;
      let params = {
        id: val.id,
        status: status,
      };
      let data = await userUpdatestatus(params);
      if (data.code == "0000") {
        this.$message({
          type: "success",
          message: data.msg,
        });
        this.getData();
      }
    },
    searchlist() {
       this.page= 1,
      this.getData();
    },
    resetform() {
      this.$refs["searchform"].resetFields();
      this.getData();
    },
    handleSizeChange(val) {
      this.rows = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    //详情
    async getquestionRead(id) {
      let data = await userRead({ id: id });
      this.dialogdata = data.data;
    },
    //获取下拉部门
    async getprepareLineOption() {
      let data = await departmentOption();
      this.prepareLineOption = data.data;
    },
    //获取下拉职务
    async getuserRoleOption() {
      let data = await userRoleOption();
      this.userRoleOption = data.data;
    },
 
    //新增
    addItem() {
      this.isshowDialog = true;
      this.dialogTitle = "新增";
      this.formtype = 0;
    },
    //编辑
    editItem(val) {
   
      this.isshowDialog = true;
      this.dialogTitle = "编辑";
      this.formtype = 1;
         this.getquestionRead(val.id);
    },

    //提交成功
    confirm() {
      this.isshowDialog = false;
      this.getData();
    },
    closedialog() {
      this.isshowDialog = false;
    },
    //获取列表
    async getData() {
      let params = {
        page: this.page,
        rows: this.rows,
      };
      if (this.form.keyWord) {
        params.keyword = this.form.keyWord;
      }
      if (this.form.department_id) {
        params.department_id = this.form.department_id;
      }
      if (this.form.status) {
        params.status = this.form.status;
      }
      console.log(params);
      let data = await userList(params);
      console.log(data);
      let datainfo = data.data;
      this.tableData = datainfo.list;
      this.pagination = datainfo.page;
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
</style>
